﻿@{
    Layout = null;
}
@*@model ZBZH_Manage_Order.Controllers.Order_ShelvesController.Zcd*@
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>订单优惠</title>
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/demo.css">
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/Styles/Order.css" rel="stylesheet" />
    <script>
        $(function () {
            GetMer();//加载操作员管理的所有商户
            //GetPayType();
            $('#btndetail').linkbutton('disable');
            $('#btnadd').linkbutton('disable');
            $('#btnedit').linkbutton('disable');
            $('#btndell').linkbutton('disable');
            $('#editDialog').dialog('close');
        })

        var Mer;
        var diplayText;//商户名称
        var SheID;
        //获取商户
        function GetMer() {
            var grid = $('#SearchMer').combogrid({
                panelWidth: 500,
                idField: 'MerID',
                textField: 'MerName',
                url: '/Order_MerPricing/getMer',
                method: 'post',
                pageSize: 5,
                pageList: [5],
                pagination: true,
                rownumbers: true,
                fitColumns: true,
                columns: [[
                    { field: 'MerID', title: '商户ID', width: 80, align: 'center' },
                    { field: 'MerName', title: '商户名称', width: 80, align: 'center' },
                    { field: 'MerTxt', title: '商户介绍', width: 120, align: 'center' },
                    { field: 'MerAdr', title: '商户地址', width: 80, align: 'center' },
                    { field: 'MerCall', title: '商户联系电话', width: 80, align: 'center' }
                ]],
                onSelect: function (index, row) {
                    diplayText = row[grid.combogrid('options').textField];
                    Mer = $('#SearchMer').combogrid("getValue");//获取操作员
                    //加载对应的商品
                    initTable(Mer);
                    //控制增删查改的权限
                    $.post("/Order_MerPricing/getPower", function (database) {
                        if (database != "") {
                            $('#btndetail').linkbutton('enable');
                            if (database.Add == 1) {
                                $('#btnadd').linkbutton('enable');
                            }
                            if (database.Dell == 1) {
                                $('#btndell').linkbutton('enable');
                            }
                            if (database.Edit == 1) {
                                $('#btnedit').linkbutton('enable');
                            }
                        }
                    })
                },
                onLoadSuccess: function () {
                    //分页后判断下是否有选中的数据行，combogrid会依据上一次选中的值进行初始化，选中datagrid数据行和设置显示值
                    if (!grid.combogrid('grid').datagrid('getSelected'))//不在当前页
                    {
                        $('#SearchMer').combogrid("setValue", diplayText);
                    }
                }
            });
        }

        //订单优惠信息datagrid
        function initTable(Mer) {
            $('#View').datagrid({
                iconCls: 'icon-user',
                loadMsg: '数据加载中...',
                fitColumns: true,
                nowrap: true,
                autoRowHeight: true,
                striped: true,
                url: '/Order_MerPricing/getgrid?MerID=' + Mer,
                border: true,
                remoteSort: false,
                singleSelect:true,
                pagination: true,
                rownumbers: true,
                columns: [[
                    { field: 'PricID', title: '订单优惠ID', sortable: true, hidden: true },
					{ field: 'MerName', title: '商户', sortable: true },
					{
					    field: 'IsOpen', title: '是否启用', formatter: function (val, rowdata, index) {
					        if (val == 0) {
					            return '不执行';
					        } else if (val == 1) {
					            return '执行';
					        }
					    }
					},
                    {
                        field: 'PricType', title: '支付模式', formatter: function (val, rowdata, index) {
                            if (val == 0) {
                                return '钱包支付';
                            } else if (val == 1) {
                                return '第三方支付';
                            }
                        }
                    },
                    { field: 'PayName', title: '支付渠道' },
                    { field: 'IsFormMon', title: '是否优惠配送费', formatter: function (val, rowdata, index) {
                        if (val == 0) {
                            return '不参与';
                        } else if (val == 1) {
                            return '参与';
                        }
                    } },
                    {
                        field: 'IsPackMon', title: '是否优惠打包费',
                        formatter: function (val, rowdata, index) {
                            if (val == 0) {
                                return '不参与';
                            } else if (val == 1) {
                                return '参与';
                            }
                        }
                    },
                    { field: 'IsMaxPric', title: '起步金额' },
                    { field: 'IsFirstMon', title: '减后执行' },
                     { field: 'MonType', title: '优惠模式',formatter: function (val, rowdata, index) {
                         if (val == 0) {
                             return '每单优惠固定金额';
                         } else if (val == 1) {
                             return '每单优惠百分比';
                         }
                     } },
                    { field: 'PricMon', title: '优惠金额/优惠百分比' },
                    {
                        field: 'PricRem', title: '小数点处理方式', formatter: function (val, rowdata, index) {
                            if (val == 0) {
                                return '四舍五入';
                            } else if (val == 1) {
                                return '舍去余数';
                            } else if (val == 2) {
                                return '余数进一';
                            } 
                        }
                    },
                    { field: 'PricTxt', title: '优惠介绍' }
                ]],
                toolbar: '#tb'
            });
        }

        //添加订单优惠信息对话框
        function AddDialog() {
            filePath = "";
            $('#editDialog').dialog('open').dialog('setTitle', '新增订单优惠信息');
            document.getElementById("ok").style.display = "block";
            ClearText();
        }

        //修改订单优惠信息对话框
        function UpdateDialog() {
            filePath = "";
            var UpdateInfoID = $("#View").datagrid("getSelections");
            if (UpdateInfoID.length == 1) {
                $("#editDialog").dialog("open").dialog("setTitle", "修改订单优惠信息");
                document.getElementById("ok").style.display = "block";
                BingUpdate();
            }
            else {
                $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + UpdateInfoID.length + "</font>行");
            }
        }

        //删除订单优惠信息对话框
        function DeleteDialog() {
            //首先获取选中的订单优惠信息的所有信息
            var checkdata = $("#View").datagrid("getSelections");//获取整行数据
            if (checkdata.length == 1) {
                var checkdata = $("#View").datagrid("getSelections")[0];//获取整行数据
                var postdata = {
                    PricID: checkdata.PricID,//订单优惠ID
                    MerID: Mer,//商户ID
                    IsOpen: checkdata.IsOpen,//是否执行
                    PricType: checkdata.PricType,//支付模式
                    PayID: checkdata.PayID,//支付ID
                    IsFormMon: checkdata.IsFormMon,//配送费是否参与优惠
                    IsPackMon: checkdata.IsPackMon,//打包费用是否参与优惠
                    IsMaxPric: checkdata.IsMaxPric,//大于这个值执行优惠
                    IsFirstMon: checkdata.IsFirstMon,//执行优惠前先减去固定金额
                    MonType: checkdata.MonType,//优惠模式
                    PricMon: checkdata.PricMon,//优惠金额
                    PricRem: checkdata.PricRem,//余数处理方式
                    PricTxt: checkdata.PricTxt,//优惠介绍
                };
                //发送异步请求删除数据
                $.messager.confirm("删除信息", "您确认删除<font color='Red' size='3'>" + "</font>该条订单优惠吗？", function (ok) {
                    if (ok)
                    {
                        $.post("/Order_MerPricing/UpdateOrder_MerPricing?Tag=" + 3, postdata, function (data) {
                            if (data == "1") {
                                $.messager.alert("友情提示", "删除成功");
                                initTable(Mer);
                            }
                            else {
                                $.messager.alert("友情提示", "删除失败:" + data);
                            }
                        });
                    }
                });
                
            }
            else {
                $.messager.alert("友情提示", "每次只能删除一行数据，你已经选择了<font color='red' size='6'>" + checkdata.length + "</font>行");
            }
        }

        //上优惠信息明细对话话框
        function DetailDialog() {
            var UpdateInfoID = $("#View").datagrid("getSelections");
            if (UpdateInfoID.length == 1) {
                $("#editDialog").dialog("open").dialog("setTitle", "优惠明细");
                BingUpdate();
                document.getElementById("ok").style.display = "none";
            }
            else {
                $.messager.alert("友情提示", "每次只能查看一行数据，你已经选择了<font color='red' size='6'>" + UpdateInfoID.length + "</font>行");
            }
        }

        //获取修改优惠信息
        function BingUpdate() {
            //首先获取选中的优惠信息
            var checkdata = $("#View").datagrid("getSelections")[0];
            //$.messager.alert("提示", checkdata.IsOpen);
            if (checkdata != null) {
                $("#PricID").textbox('setValue', checkdata.PricID)
                $("#MerID").textbox('setValue',Mer)
                if (checkdata.IsOpen == 1) {
                    $("#IsOpen").attr('checked', true);
                }
                else {
                    $("#IsOpen").attr('checked', false) ;
                }
                $("#PricType").combobox('setValue', checkdata.PricType)
                GetPayType(checkdata.PayID);
                $("#PayID").combobox('setValue', checkdata.PayID);
                //if (checkdata.IsFormMon == 0)
                //{ $("#IsFormMon").attr("checked", ''); }
                if (checkdata.IsFormMon == 1) {
                    $("#IsFormMon").attr("checked", true);
                }
                else {
                    $("#IsFormMon").attr("checked", false);
                }
                //if (checkdata.IsPackMon == 0)
                //{ $("#IsPackMon").attr("checked", ''); }
                if (checkdata.IsPackMon == 1) {
                    $("#IsPackMon").attr("checked", true);
                }
                else {
                    $("#IsPackMon").attr("checked", false);
                }
                if (checkdata.MonType == "1") {
                    //alert(checkdata.PricMon + "=" + checkdata.PricMon / 10);
                    $("#PricMon").numberbox('setValue', checkdata.PricMon/10);
                }
                else {
                    $("#PricMon").numberbox('setValue', checkdata.PricMon);
                }
                $("#IsMaxPric").numberbox('setValue', checkdata.IsMaxPric);
                $("#IsFirstMon").numberbox('setValue', checkdata.IsFirstMon);
                $("#MonType").combobox('setValue', checkdata.MonType);
               
                $("#PricRem").combobox('setValue', checkdata.PricRem);
                $("#PricTxt").val(checkdata.PricTxt);
            }
        }

        //清空优惠信息
        function ClearText() {
            //首先获取选中的优惠信息
            $("#PricID").textbox('setValue', "");
            $("#IsOpen").attr("checked", "");
            $("#PricType").combobox('setValue', "0");
            GetPayType();
            $("#PayID").combobox('setValue', null);
            $("#IsFormMon").attr("checked", "");
            $("#IsPackMon").attr('checked', "");
            $("#IsMaxPric").numberbox('setValue', "0");
            $("#IsFirstMon").numberbox('setValue', "0")
            $("#MonType").combobox('setValue', "0")
            $("#PricMon").numberbox('setValue', "0")
            $("#PricRem").combobox('setValue', "0")
            $("#PricTxt").val("")
        }

        //保存
        function submitForm() {
            var IsOpen = 0;
            var a=  document.getElementById("IsOpen");
            if (a.checked)
            {
                IsOpen = 1;
            }
            var IsFormMon = 0;
            var b = document.getElementById("IsFormMon");
            if (b.checked) {
                IsFormMon = 1;
            }
            var IsPackMon = 0;
            var c = document.getElementById("IsPackMon");
            if (c.checked) {
                IsPackMon = 1;
            }
            var postdata = {
                PricID: $("#PricID").textbox('getValue'),//订单优惠ID
                MerID:Mer,//商户ID
                IsOpen:IsOpen,//是否执行
                PricType: $("#PricType").combobox('getValue'),//支付模式
                PayID: $("#PayID").combobox('getValue'),//支付ID
                IsFormMon: IsFormMon,//配送费是否参与优惠
                IsPackMon: IsPackMon,//打包费用是否参与优惠
                IsMaxPric: $("#IsMaxPric").numberbox('getValue'),//大于这个值执行优惠
                IsFirstMon: $("#IsFirstMon").numberbox('getValue'),//执行优惠前先减去固定金额
                MonType: $("#MonType").combobox('getValue'),//优惠模式
                PricMon: $("#PricMon").numberbox('getValue'),//优惠金额
                PricRem: $("#PricRem").combobox('getValue'),//余数处理方式
                PricTxt: $("#PricTxt").val()//优惠介绍
            };
            var Tag = 1;//新增
            //alert("PricID:" + postdata.PricID);
                //如果上架ID不为空
                if (postdata.PricID != "") {
                    Tag = 2;//修改
                }
                if ($("#MonType").combobox('getValue') == "1" && $("#PricMon").numberbox('getValue')>100) {
                    $.messager.alert("友情提示", "优惠折扣已超售价，请检查");
                    return;
                }
                //使用异步实现修改用户信息
                $.post("/Order_MerPricing/UpdateOrder_MerPricing?Tag=" + Tag, postdata, function (date) {
                    if (date == "1") {
                        $("#editDialog").dialog('close');
                        $("#View").datagrid('reload');
                    }
                    else {
                        $.messager.alert("友情提示", "修改失败，请检查");
                    }
                });
        }

        //获取支付渠道
        function GetPayType(PayID) {
            $.get("/Order_MerPricing/getOrderTPayType?PricType=" + $("#PricType").combobox('getValue') + "&PayID=" + PayID, {}, function (database) {
                $('#PayID').combobox({
                    valueField: 'PayID',
                    textField: 'PayName',
                    data: database
                });
            })
        }

    </script>
</head>
<body style="padding: 0;">
    <div style="padding: 5px; background-color: #F4F4F4">
        <div>
            <a>商户：</a>
            <input id="SearchMer" class="easyui-combogrid" style="width: 120px" />
        </div>
    </div>
    <div>
        <table id="View"></table>
    </div>
    <div id="tb" style="padding: 5px; height: auto;background-color: #F4F4F4">
        <div style="margin-bottom: 5px">
           <a href="#" class="easyui-linkbutton" id="btndetail" iconcls="icon-more" onclick="DetailDialog()" plain="true">明细</a>
            <a href="#" class="easyui-linkbutton" id="btnadd" iconcls="icon-add" onclick="AddDialog()" plain="true">新增</a>
            <a href="#" class="easyui-linkbutton" id="btnedit" iconcls="icon-edit" onclick="UpdateDialog()" plain="true">编辑</a>
            <a href="#" class="easyui-linkbutton" id="btndell" iconcls="icon-cancel" onclick="DeleteDialog()" plain="true">删除</a>
        </div>
    </div>

    <div id="editDialog" class="easyui-dialog" style="width: 600px; padding-top:10px; padding-bottom:10px; padding:fixed; top:5px;" closed="true"; resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="editController" method="post" novalidate="novalidate">
            <table width="500" id="tbledit">
                 <tr hidden="hidden">
                    <td class="userinfo-td-l">PricID：</td>
                    <td  style="margin-right:0px;padding-right:0px;" colspan="2">
                <input type="text" id="PricID" class="easyui-textbox" name="PricID" />
                    </td>
                </tr>
                 <tr>
                    <td class="userinfo-td-l">是否启用：</td>
                    <td  style="margin-right:0px;padding-right:0px;" colspan="2">
                <input type="checkbox" id="IsOpen" name="IsOpen" />
                    </td>
                </tr>
                <tr>
                    <td class="userinfo-td-l">支付模式：</td>
                    <td  style="margin-right:0px;padding-right:0px;">
                  <select class="easyui-combobox" type="text" id="PricType" name="PricType" data-options="onSelect: function(){GetPayType()}">
                      <option id="PricType0" value="0">钱包支付</option>
                       <option id="PricType1" value="1">第三方支付</option>
                  </select>
                    </td>
                    <td style="padding-left:0px;margin:0px"> <input class="easyui-combobox" type="text" id="PayID"  name="PayID"/></td>
                </tr>
                <tr></tr>
                 <tr>
                    <td class="userinfo-td-l">参与优惠：</td>
                    <td >
                        <input type="checkbox" id="IsFormMon" name="IsFormMon" />配送费
                        <input type="checkbox" id="IsPackMon" name="IsPackMon" />包装费
                    </td>
                </tr>
                <tr></tr>
                <tr>
                    <td class="userinfo-td-l">起步金额：</td>
                    <td >
                        <input type="text" class="easyui-numberbox" id="IsMaxPric" name="IsMaxPric" data-options="min:0,precision:2"/>
                   </td>
                    <td>元</td>
                </tr>
                <tr></tr>
                 <tr>
                    <td class="userinfo-td-l">优惠下限：</td>
                    <td >
                        <input type="text" class="easyui-numberbox" id="IsFirstMon" name="IsFirstMon" data-options="min:0,precision:2"/>
                   </td>
                     <td>元</td>
                </tr>
                <tr></tr>
                 <tr>
                    <td class="userinfo-td-l">优惠模式：</td>
                    <td style="width:148px">
                        <select type="text" class="easyui-combobox" id="MonType" name="MonType">
                            <option id="MonType0" value="0">固定价格优惠</option>
                            <option id="MonType1" value="1">折扣优惠</option>
                        </select>
                   </td>
                     <td style="width:148px"><input type="text" class="easyui-numberbox" id="PricMon" name="PricMon" data-options="min:0,precision:0"/></td>
                      <td>元/折</td>
                </tr>
                <tr></tr>
                 <tr>
                    <td class="userinfo-td-l">余数处理：</td>
                    <td  colspan="3">
                        <select class="easyui-combobox" style="width: 400px" id="PricRem" name="PricRem">
                            <option id="0" value="0">四舍五入</option>
                            <option id="1" value="1">舍去余数</option>
                            <option id="2" value="2">余数进一</option>
                        </select>
                    </td>
                </tr>
                <tr></tr>
                <tr>
                    <td class="userinfo-td-l">优惠介绍：</td>
                    <td  colspan="3">
                        <textarea class="easyui-textarea" rows="5" style="width: 400px" id="PricTxt" name="PricTxt"></textarea>
                    </td>
                </tr>
                </table>
            <br />
            <div align="right" style="padding-right:50px">
            <a href="javascript:void(0)" class="easyui-linkbutton"  id="ok" iconcls="icon-ok" style="width:60px;height:20px" onclick="submitForm()">保存</a></div>
        </form>
    </div>
</body>
</html>
